package cate.game.pvp.gradepvp;

import cate.game.framework.ModuleMan;
import cate.game.play.part.PlaySide;
import cate.game.pvp.gradepvp.dto.GradePvpFightOverParam;
import cate.game.pvp.gradepvp.dto.GradePvpMatchParam;
import cate.game.pvp.gradepvp.dto.GradePvpPlayerData;
import cate.game.role.RoleSimpleBase;
import cate.game.role.pvp.gradepvp.GradePvpRecord;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("unchecked")
@Slf4j
public class GradePvpMsgManager extends ModuleMan {
    @Override
    public void initDependent() throws Exception {

    }

    public void fightOver(PlaySide a, int star, int score, GradePvpRecord record) {
        try {
            game.rpcSend().fireAndForgetGroup(
                    GradePvpMsg.FIGHT_OVER,
                    GradePvpFightOverParam.of(a, star, score, record)
            );
        } catch (Exception e) {
            log.error("段位赛 FIGHT_OVER 失败，", e);
        }
    }

    public GradePvpPlayerData match(String playerUid, int star) {
        try {
            return game.rpcSend().requestResponseGroup(
                    GradePvpMsg.MATCH_REQ,
                    GradePvpMatchParam.of(playerUid, star),
                    GradePvpPlayerData.class);
        } catch (Exception e) {
            log.error("段位赛 MATCH_REQ 失败", e);
        }
        return null;
    }

    public List<GradePvpRecord> loadNewBeeRecords() {
        try {
            return game.rpcSend().requestResponseGroup(
                    GradePvpMsg.NEW_BEE_RECORDS,
                    null,
                    ArrayList.class
            );
        } catch (Exception e) {
            log.error("段位赛 NEW_BEE_RECORDS 失败", e);
        }
        return Collections.emptyList();
    }

    public List<RoleSimpleBase> loadSixGods(int season) {
        try {
            return game.rpcSend().requestResponseGroup(
                    GradePvpMsg.SIX_GOD,
                    season,
                    ArrayList.class
            );
        } catch (Exception e) {
            log.error("段位赛 SIX_GOD 失败,", e);
        }
        return Collections.emptyList();
    }

    public int loadRank(String roleUid) {
        try {
            return game.rpcSend().requestResponseGroup(
                    GradePvpMsg.MY_RANK,
                    roleUid,
                    Integer.class
            );
        } catch (Exception e) {
            if(StringUtils.equals("no executable client", e.getMessage())) {
                log.error("段位赛 MY_RANK 失败 。fax 提示 {} 可能是关联的跨服尚未启动", e.getMessage());
            } else {
                log.error("段位赛 MY_RANK 失败", e);
            }
        }
        return -1;
    }
}
